package cn.lili.modules.goods.entity.dos;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.goods.entity.enums.SalesModeEnum;
import cn.lili.mybatis.model.BaseSceneEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 草稿商品
 *
 * @author pikachu
 * @since 2020-02-23 9:14:33
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("li_draft_goods")
@ApiModel(value = "草稿商品")
@AllArgsConstructor
@NoArgsConstructor
public class DraftGoods extends BaseSceneEntity {


    private static final long serialVersionUID = 466771876880180330L;


    @ApiModelProperty(value = "商品名称")
    @NotEmpty(message = "商品名称不能为空")
    @Length(max = 100, message = "商品名称太长，不能超过100个字符")
    private String goodsName;

    @ApiModelProperty(value = "商品价格", required = true)
    @NotNull(message = "商品价格不能为空")
    @Min(value = 0, message = "商品价格不能为负数")
    @Max(value = 99999999, message = "商品价格不能超过99999999")
    private Double price;

    @ApiModelProperty(value = "品牌id")
    private String brandId;

    @ApiModelProperty(value = "分类path")
    private String categoryPath;

    @ApiModelProperty(value = "计量单位")
    private String goodsUnit;


    @Length(max = 60, message = "商品卖点太长，不能超过60个字符")
    @ApiModelProperty(value = "卖点")
    private String sellingPoint;

    /**
     * @see GoodsMarketEnum
     */
    @ApiModelProperty(value = "上架状态")
    private String marketEnable;

    @ApiModelProperty(value = "详情")
    private String intro;

    @ApiModelProperty(value = "购买数量")
    private Integer buyCount;

    @Max(value = 99999999, message = "库存不能超过99999999")
    @ApiModelProperty(value = "库存")
    private Integer quantity;

    @ApiModelProperty(value = "商品好评率")
    private Double grade;

    @ApiModelProperty(value = "缩略图路径")
    private String thumbnail;

    @ApiModelProperty(value = "小图路径")
    private String small;

    @ApiModelProperty(value = "原图路径")
    private String original;

    @ApiModelProperty(value = "店铺分类id")
    private String storeCategoryPath;

    @ApiModelProperty(value = "评论数量")
    private Integer commentNum;

    @ApiModelProperty(value = "卖家id")
    private String storeId;

    @ApiModelProperty(value = "卖家名字")
    private String storeName;

    @ApiModelProperty(value = "运费模板id")
    private String templateId;

    /**
     * @see GoodsAuthEnum
     */
    @ApiModelProperty(value = "审核状态")
    private String authFlag;

    @ApiModelProperty(value = "审核信息")
    private String authMessage;

    @ApiModelProperty(value = "下架原因")
    private String underMessage;

    @ApiModelProperty(value = "是否自营")
    private Boolean selfOperated;

    @ApiModelProperty(value = "商品移动端详情")
    private String mobileIntro;

    @ApiModelProperty(value = "商品视频")
    private String goodsVideo;


    @ApiModelProperty(value = "是否为推荐商品", required = true)
    private Boolean recommend;

    /**
     * @see SalesModeEnum
     */
    @ApiModelProperty(value = "销售模式", required = true)
    private String salesModel;


    /**
     * @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
     */
    @ApiModelProperty(value = "商品类型", required = true)
    private String goodsType;

    @ApiModelProperty(value = "商品参数json", hidden = true)
    private String params;

    @ApiModelProperty(value = "存入索引目标，为空则应该是默认索引", hidden = true)
    private String esIndex;


    @ApiModelProperty(value = "供应商ID")
    private String supplierId;

    @ApiModelProperty(value = "供应商名称")
    private String supplierName;

    @ApiModelProperty(value = "供应商商品ID")
    private String supplierGoodsId;

    @ApiModelProperty(value = "支持代发")
    private Boolean supportProxy;

    @ApiModelProperty(value = "支持采购")
    private Boolean supportPurchase;

    /**
     * @see cn.lili.modules.goods.entity.enums.PurchaseRuleEnum
     */
    @ApiModelProperty(value = "采购规则")
    private String purchaseRule;

    /**
     * @see cn.lili.modules.goods.entity.enums.SupplierEnum
     */
    @ApiModelProperty(value = "供应商类型")
    private String supplierEnum;


    @ApiModelProperty(value = "保留字段1", hidden = true)
    private String field1;

    @ApiModelProperty(value = "保留字段2", hidden = true)
    private String field2;

    @ApiModelProperty(value = "保留字段3", hidden = true)
    private String field3;

    @ApiModelProperty(value = "保留字段4", hidden = true)
    private String field4;

    @ApiModelProperty(value = "保留字段5", hidden = true)
    private String field5;

    @ApiModelProperty(value = "扩展字段，可自由存储，数据库为text格式", hidden = true)
    private String ext;

    @ApiModelProperty(value = "是否是代理商品")
    private Boolean isProxyGoods = false;

    public DraftGoods(GoodsOperationDTO goodsOperationDTO) {
        BeanUtils.copyProperties(goodsOperationDTO, this);


        if (goodsOperationDTO.getGoodsParamsDTOList() != null && goodsOperationDTO.getGoodsParamsDTOList().isEmpty()) {
            this.params = JSONUtil.toJsonStr(goodsOperationDTO.getGoodsParamsDTOList());
        }
        //如果立即上架则
        this.marketEnable = Boolean.TRUE.equals(goodsOperationDTO.getRelease()) ? GoodsMarketEnum.UPPER.name() : GoodsMarketEnum.DOWN.name();

        this.grade = 100D;

        if (StringUtil.isNotEmpty(goodsOperationDTO.getGoodsId())) {
            this.setId(goodsOperationDTO.getGoodsId());
        }

    }

    public Boolean getSupportProxy() {
        if (supportProxy == null) {
            return false;
        }
        return supportProxy;
    }

    public Boolean getSupportPurchase() {
        if (supportPurchase == null) {
            return false;
        }
        return supportPurchase;
    }

    public Boolean getIsProxyGoods() {
        if (isProxyGoods == null) {
            return false;
        }
        return isProxyGoods;
    }

    public String getIntro() {
        if (CharSequenceUtil.isNotEmpty(intro)) {
            return HtmlUtil.unescape(intro);
        }
        return intro;
    }

    public String getMobileIntro() {
        if (CharSequenceUtil.isNotEmpty(mobileIntro)) {
            return HtmlUtil.unescape(mobileIntro);
        }
        return mobileIntro;
    }

}